package com.itheima.chapter11.tcp.case3;

import java.io.*;
import java.net.Socket;

/*
TCP上传文件  线程池
 */
public class Clinet {
    public static void main(String[] args) throws IOException {
        //创建socket对象，指定IP端口
        Socket socket = new Socket("127.0.0.1", 9997);
        //获取传输数据的流对象(网络流)
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

        //写出文件名
        File file = new File("C:\\省考报名照片.jpg");
        bw.write(file.getName());
        bw.newLine();
        bw.flush();

        //读取服务器端回写消息
        String flag = br.readLine();
        if ("okk".equals(flag)) {
            //创建本地字节输入流，关联要上传的文件
            FileInputStream fis = new FileInputStream(file);
            //上传文件字节
            byte[] bys = new byte[1024];
            int len;
            while ((len = fis.read(bys)) != -1) {
                os.write(bys,0,len);
            }
            fis.close();
            //给服务器一个结束标记
            socket.shutdownOutput();
        }
        String result = br.readLine();
        System.out.println(result);

        socket.close();

    }
}
